package com.sibrary.booxoo;

import java.util.List;

import com.sibrary.booxoo.download.BooxooDownloadController;
import com.sibrary.booxoo.download.BooxooDownloadEventController;
import com.sibrary.booxoo.download.BooxooDownloadItem;
import com.sibrary.booxoo.download.BooxooIDownloadListener;
import com.sibrary.utils.Constants;
import com.sibrary.utils.DomUtils;

import android.R.bool;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class BooxooDownloadActivity extends Activity implements
		BooxooIDownloadListener {

	private DownloadAdapter mDownloadAdapter;
	private ListView mDownloadsListView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.booxoo_download_activity);

		mDownloadsListView = (ListView) findViewById(R.id.DownloadActivity_downloadListView);
		mDownloadAdapter = new DownloadAdapter(this,
				BooxooDownloadController.getInstance().mDownloads);

		mDownloadsListView.setAdapter(mDownloadAdapter);
	}

	@Override
	protected void onResume() {
		super.onResume();

		BooxooDownloadEventController.getInstance().addListener(this);
	}

	@Override
	protected void onPause() {
		super.onPause();

		BooxooDownloadEventController.getInstance().removeListener(this);
	}

	@Override
	public void onDownloadEvent(int type, Object obj) {
		final BooxooDownloadItem item;
		if (obj != null) {
			item = (BooxooDownloadItem) obj;
		} else {
			return;
		}
		
		switch (type) {
		case Constants.MESSAGE_DOWNLOAD_START: {
			item.setProgress(Constants.PROGRESS_MIN);
			mDownloadAdapter.notifyDataSetChanged();
			break;
		}
		case Constants.MESSAGE_DOWNLOAD_RUNNING: {
			mDownloadAdapter.notifyDataSetChanged();
			break;
		}
		case Constants.MESSAGE_DOWNLOAD_FINISH: {
			DomUtils.deleteItemInXml(item);
			item.setProgress(Constants.PROGRESS_MAX);
			item.setFinished(true);
			mDownloadAdapter.notifyDataSetChanged();
			break;
		}
		case Constants.MESSAGE_DOWNLOAD_CANCEL: {
			DomUtils.alterItemInXml(item);
			item.setProgress(Constants.PROGRESS_MAX);
			mDownloadAdapter.notifyDataSetChanged();
			break;
		}
		default:
			break;
		}
	}

	public class DownloadAdapter extends BaseAdapter {

		private Context context;

		// 数据源
		private List<BooxooDownloadItem> mAdapterDownloads;

		// 构造函数
		public DownloadAdapter(Context context,
				List<BooxooDownloadItem> downloads) {
			this.context = context;
			this.mAdapterDownloads = downloads;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mAdapterDownloads.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mAdapterDownloads.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			final BooxooDownloadItem item = mAdapterDownloads.get(position);

			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(
						R.layout.booxoo_download_item, null);

				holder = new ViewHolder();
				holder.mFilenameTextView = (TextView) convertView
						.findViewById(R.id.DownloadItem_downloadFilenameText);
				holder.mUrlTextView = (TextView) convertView
						.findViewById(R.id.DownloadItem_downloadUrlText);
				holder.mProgressBar = (ProgressBar) convertView
						.findViewById(R.id.DownloadItem_downloadProgressBar);
				holder.mCancelBtn = (Button) convertView
						.findViewById(R.id.DownloadItem_downloadCancelBtn);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.mFilenameTextView.setText(item.getFilename());
			holder.mUrlTextView.setText(item.getUrl());
			holder.mProgressBar.setProgress(item.getProgress());

			final Button btn = holder.mCancelBtn;
			if (item.getFinished()) {
				btn.setText(R.string.DownloadItem_downloadButtonOk);
			} else {
				if (item.getCancel()) {
					btn.setText(R.string.DownloadItem_downloadButtonDownload);
				} else {
					btn.setText(R.string.DownloadItem_downloadButtonCancel);
				}
			}

			// 该btn点击频繁时， 容易出现线程不同步问题， 就造成比较多的其他问题， 待解决
			btn.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					if (item.getFinished()) {
						// FIXME: Nothing to do
						return;
					}

					if (item.getCancel()) { // FIXME: 重新下载
						item.setCancel(false);
						item.setFinished(false);
						BooxooDownloadController.getInstance().addItem(item);
						BooxooDownloadController.getInstance().alterItem(item);
						BooxooDownloadController.getInstance().notifyStart();
					} else { // FIXME: 取消下载
						item.setCancel(true);
						item.setFinished(false);
						item.abort();
					}
				}
			});

			return convertView;
		}
	}

	public class ViewHolder {
		public TextView mFilenameTextView;
		public TextView mUrlTextView;
		public Button mCancelBtn;
		public ProgressBar mProgressBar;
	}
}
